package com.weitu.permission.authority.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.base.entity.Tree;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.authority.entity.Authority;
import com.weitu.permission.authority.service.AuthorityService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WT on 2017/3/17.
 */


@Controller
@RequestMapping(value = "permission/authority")
public class AuthorityController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/authority";
    private final AuthorityService authorityService;

    @Autowired
    public AuthorityController(AuthorityService authorityService) {
        this.authorityService = authorityService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    /**
     * 权限分页列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        putParametersIntoMap(map);

        //查询数据总数
        int count = this.authorityService.queryTotalCount(map);
        //获取分页对象
        DataGridPagination Pagination = DataGridPagination.getInstance(request, count);

        PageView pageView = Pagination.getPageView();//生成pageView分页对象
        //未传sort参数是采用添加时间排序
        if (map.get("sort") == null) {
          pageView.setSortColumn("addTime");
        }
        map.put("pageView", pageView);

        List<Authority> dataList = this.authorityService.queryAuthorityList(map);

        return Pagination.flushDataGridData(dataList);
    }


    /**
     * 获取添加页面
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addView() {
        return MODULE_BATH_PATH + "/add";
    }



    /**
     * 添加权限
     *
     * @param authority
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "权限管理",methods = "添加权限")
    public AjaxResult insertSelective(Authority authority) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        AjaxResult result = new AjaxResult();
        String id = IDGenerator.getId();
        String addTime = DateTimeUtil.formatterDateTime();
        authority.setId(id);
        authority.setAddTime(addTime);
        int boo = authorityService.insertSelective(authority);
        if (boo == 1) {
            result.setMessage("保存成功");
            result.setSuccess(true);
            return result;
        } else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }


    /**
     * 删除操作
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "权限管理",methods = "删除权限")
    public AjaxResult deleteById(String id) {
        AjaxResult result = new AjaxResult();
        int boo = authorityService.deleteById(id);
        if (boo == 1) {
            result.setMessage("删除成功");
            result.setSuccess(true);
            return result;
        } else {
            result.setMessage("删除失败！");
            result.setSuccess(false);
            return result;
        }
    }


    /**
     * 获取更新数据
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        Authority authority = authorityService.selectAuthorityById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("authority", authority);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");

        return modelAndView;
    }

    /**
     * 保存更新
     */
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Transactional(readOnly = false)//需要事务操作必须加入此注解
    @SystemLog(module = "权限管理",methods = "编辑权限")
    public AjaxResult edit(Authority authority) {
        AjaxResult result = new AjaxResult();
        String updateTime = DateTimeUtil.formatterDateTime();
        authority.setUpdateTime(updateTime);
        int boo = authorityService.updateByIdSelective(authority);
        if (boo == 1) {
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        } else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }


    /**
     *获取权限树
     * @return
     */
    @ResponseBody
@RequestMapping(value = "getAuthorityTree",method = RequestMethod.POST)
  public List<Tree> AuthorityTree(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        return this.authorityService.getAuthorityTree(paramMap);
}

}



